<template>
  <el-dialog title="修改用户" :visible.sync="ed_Dialogvisible" width="50%" @close="editDialogClose">
    <!-- 内容主题区域 -->
    <el-form :model="editForm" :rules="editFormRules" ref="editFormref" label-width="70px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="editForm.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="editForm.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="editForm.mobile"></el-input>
      </el-form-item>
    </el-form>
    <!-- 底部区域 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="editDialogClose">取 消</el-button>
      <el-button type="primary" @click="edituserinfo">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    // 自定义验证邮箱
    var checkEmail = (rele, value, cb) => {
      const regEmail = /^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+([a-zA-Z0-9_-])+/

      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error('请输入合法的邮箱！'))
    }
    // 自定义验证手机号
    var checkMobile = (rele, value, cb) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error('请输入合法的手机！'))
    }
    return {
      ed_Dialogvisible: false,
      // 查询到的用户对象
      editForm: {},
      // 修改表单的验证规则对象
      editFormRules: {
        email: [{
          required: true,
          message: '请输入邮箱',
          trigger: 'blur'
        }, {
          validator: checkEmail,
          trigger: 'blur'
        }],
        mobile: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          validator: checkMobile,
          trigger: 'blur'
        }]
      }
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    getUserList: {
      type: Function,
      default: null
    },
    id: {
      type: Number,
      default: 1
    }
  },
  watch: {
    visible (val, oldVal) {
      this.ed_Dialogvisible = val
    },
    id (val, oldVal) {
      console.log(val)
      this.showEditDialog(val)
    }
  },
  methods: {
    // 展示编辑用户的对话框
    showEditDialog (id) {
      // 根据id获取用户数据
      this.$http.get('/users/' + id)
        .then((res) => {
          console.log(res.data)
          if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)
          this.editForm = res.data.data
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 监听用户修改对话框的关闭事件
    editDialogClose () {
      this.$emit('update:visible', false)
    },
    // 修改用户信息并提交
    edituserinfo () {
      this.$refs.editFormref.validate((valid) => {
        console.log(15151515)
        if (!valid) return
        // 发起用户修改信息数据请求
        this.$http.put('/users/' + this.editForm.id, {
          email: this.editForm.email,
          mobile: this.editForm.mobile
        })
          .then((res) => {
            // console.log(res.data)
            if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)
            // 关闭对话框
            this.editDialogClose()
            // 刷新数据列表
            this.getUserList()
            // 提示修改成功
            this.$message.success(res.data.meta.msg)
          })
          .catch((error) => {
            console.log(error)
          })
      })
    }
  }
}
</script>
<style>

</style>
